package com.app.micaihu.custom.view;

import android.content.Context;
import android.net.Uri;
import android.util.AttributeSet;
import android.view.ViewGroup;

import com.app.micaihu.R;
import com.app.utils.util.ViewSizeUtils;
import com.facebook.drawee.backends.pipeline.Fresco;
import com.facebook.drawee.drawable.ScalingUtils;
import com.facebook.drawee.generic.GenericDraweeHierarchy;
import com.facebook.drawee.generic.GenericDraweeHierarchyBuilder;
import com.facebook.drawee.generic.RoundingParams;
import com.facebook.imagepipeline.request.ImageRequestBuilder;


/**
 * Created by Guo on 2016/12/2.
 * <p>
 * 可以直接限制大小的圆形view
 */

public class CircleImageOnly extends CustomImageView {

    private String url;
    private int radius;
    private int color;
    private ViewGroup.LayoutParams layoutParams;

    public CircleImageOnly(Context context, String url, int radius) {
        super(context);
        this.url = url;
        this.radius = radius;

        init();
    }

    public CircleImageOnly(Context context, String url, int radius, int color) {
        super(context);
        this.url = url;
        this.radius = radius;
        this.color = color;

        init();
    }

    public CircleImageOnly(Context context, AttributeSet attrs) {
        super(context, attrs);
        init();
    }

    public CircleImageOnly(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        init();
    }

    private void init() {
        GenericDraweeHierarchy hierarchy = null;
        boolean b = this.hasHierarchy();
        RoundingParams roundingParams;
        if (color <= 0) {
            roundingParams = RoundingParams.asCircle()
                    .setBorderWidth(3)
                    .setBorderColor(getResources().getColor(R.color.white));

        } else {
            roundingParams = RoundingParams.asCircle()
                    .setBorderWidth(3)
                    .setBorderColor(getResources().getColor(R.color.white))
                    .setOverlayColor(getResources().getColor(color))//设置覆盖层颜色,这里设置为白色，xml中的属性设置会失效，以这里的属性为准
                    .setRoundingMethod(RoundingParams.RoundingMethod.OVERLAY_COLOR);
        }


        if (b) {
            hierarchy = this.getHierarchy();
            hierarchy.setFadeDuration(0);
            hierarchy.setActualImageScaleType(ScalingUtils.ScaleType.FIT_XY);
            hierarchy.setRoundingParams(roundingParams);
            hierarchy.setOverlayImage(null);
        } else {
            GenericDraweeHierarchyBuilder builder = new GenericDraweeHierarchyBuilder(getContext().getResources());
            builder.setFadeDuration(0);
            builder.setActualImageScaleType(ScalingUtils.ScaleType.FIT_XY);
            builder.setRoundingParams(roundingParams);
            builder.setOverlay(null);

            hierarchy = builder.build();
        }

        this.setHierarchy(hierarchy);

        this.setController(Fresco.newDraweeControllerBuilder()
                .setImageRequest(ImageRequestBuilder.newBuilderWithSource(Uri.parse(url)).build())//设置请求
                .setOldController(getController())
                .build());

        if (layoutParams == null) {
            layoutParams = new ViewGroup.LayoutParams(ViewSizeUtils.dp2px(getContext(), radius), ViewSizeUtils.dp2px(getContext(), radius));

        }

        this.setLayoutParams(layoutParams);

        requestLayout();


    }


}
